package com.xrh.xdemo.service.controllers;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xrh.core.annotation.RepeatRequest;
import com.xrh.core.bean.CommonPage;
import com.xrh.core.bean.CommonResult;
import com.xrh.core.enums.ResultCodeEnum;
import com.xrh.core.exceptions.BusinessException;

import com.xrh.xdemo.api.dto.entity.MenuDTO;
import com.xrh.xdemo.api.dto.entity.MenuTreeDTO;
import com.xrh.xdemo.api.request.addRequest.MenuAddRequest;
import com.xrh.xdemo.api.request.queryRequest.MenuQueryRequest;
import com.xrh.xdemo.api.request.updateRequest.MenuUpdateRequest;
import com.xrh.xdemo.service.annotations.OperateTrace;
import com.xrh.xdemo.service.service.MenuService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags="菜单API")
@RestController
@RequestMapping("menu")
@Slf4j
public class MenuController {

    @Resource
    private MenuService menuService;

    /**
   	 * @function 获取用户菜单
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("获取用户菜单")
   	@PostMapping("/getUserMenu")
   	public  CommonResult<List<MenuTreeDTO>> getUserMenu(){
   		
   		try {
   		
   			return CommonResult.success(menuService.getUserMenu());
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("getUserMenu 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		}
   		
   	}
   	
    /**
   	 * @function 获取系统菜单
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("获取系统菜单")
   	@PostMapping("/getSysMenu")
   	public  CommonResult<List<MenuTreeDTO>> getSysMenu(){
   		
   		try {
   		
   			return CommonResult.success(menuService.findMenuTreeList());
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("getSysMenu 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		}
   		
   	}
   	
    /**
   	 * @function 获取角色绑定的系统菜单
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("获取角色绑定的系统菜单")
   	@PostMapping("/getRoleBindSysMenu")
   	@ApiImplicitParam(name = "roleId" , value = "角色id")
   	public  CommonResult<List<MenuTreeDTO>> getRoleBindSysMenu(Long roleId){
   		
   		try {
   		
   			return CommonResult.success(menuService.findRoleBindMenuTreeList(roleId));
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("getRoleBindSysMenu 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		}
   		
   	}
    
    /**
   	 * @function 新增Menu
   	 * @param menuAddRequest
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("新增Menu")
   	@PostMapping("/addMenu")
   	@RepeatRequest(timeout = 2)
   	@OperateTrace(operateType = "新增" , businessType = "菜单" , businessNoKeyPath = "data" , businessNoFromReturnResult = true ,  titleKeyPath = "name" )
   	public  CommonResult<Long> addMenu(@Valid @RequestBody MenuAddRequest menuAddRequest){
   		
   		try {
   		
   			Long id = menuService.addMenu(menuAddRequest);
   			
   			return CommonResult.success(id);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("addMenu 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		}
   		
   	}
   	
   	/**
   	 * @function 删除Menu
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("删除Menu")
   	@ApiImplicitParam(name = "id", value = "主键ID", required = true)
   	@PostMapping("/deleteMenuById")
   	@OperateTrace(operateType = "删除" , businessType = "菜单" , businessNoKeyPath = "id")
   	public CommonResult<Void> deleteMenuById(Long  id){
   		
   		try {
   			
   			if(id == null)
   				return CommonResult.failed("id不能为空");
   			
   			menuService.deleteMenuById(id);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("deleteMenu发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}
   	
   	
   	/**
   	 * @function 更新Menu
   	 * @param menuUpdateRequest
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("更新Menu")
   	@PostMapping("/updateMenu")
   	@OperateTrace(operateType = "修改" , businessType = "菜单" , businessNoKeyPath = "id" ,  titleKeyPath = "roleName" )
   	public  CommonResult<Void> updateMenu(@Valid @RequestBody MenuUpdateRequest menuUpdateRequest){
   		
   		try {
   			
   			menuService.updateMenu(menuUpdateRequest);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("updateMenu 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}
   	
   	/**
   	 * @function 根据ID获取对象
   	 * @param id
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("获取Menu信息")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true)
   	@PostMapping("/findMenuById")
   	public CommonResult<MenuDTO> findMenuById(Long id){
   		
   		try {
   			
   			if(id == null)
   				return CommonResult.failed("id不能为空");
   				
   			MenuDTO dto =  menuService.findMenuById(id);
   			
   			return CommonResult.success(dto);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("findMenuById 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   		
   	}
   	
   	/**
   	 * @function 查询Menu列表
   	 * @author 肖荣辉
   	 * @param menuQueryRequest
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("获取Menu列表")
   	@PostMapping("/findMenuList")
   	public CommonResult<CommonPage<MenuDTO>> findMenuList(@RequestBody  MenuQueryRequest menuQueryRequest){
   		
   		try {
   			 
   			return menuService.findMenuList(menuQueryRequest);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("findMenuList 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}

}
